Texas Residents on Public Health Insurance

Source: U.S. Census Bureau, ACS 5-year, Table B27015 and B01003 Table 1 title: HEALTH INSURANCE COVERAGE STATUS AND TYPE BY HOUSEHOLD INCOME IN THE PAST 12 MONTHS, https://data.census.gov/cedsci/table?q=B27015

Table 2 title: TOTAL POPULATION, https://data.census.gov/cedsci/table?q=b01003

# Reference: https://walkerke.github.io/tidycensus/articles/basic-usage.html
# Reference: https://juliasilge.com/blog/using-tidycensus/

# View Census data tables available
#View(load_variables(2017, "acs5", cache = TRUE))

# persons on public health insurance from census in Texas counties
public.HI <- get_acs(geography = "county", 
                     variables = c("B27015_002"), 
                     state = "TX",
                     geometry = FALSE,
                     year = 2017)
## Getting data from the 2013-2017 5-year ACS
# population of Texas counties
pop <- get_acs(geography = "county", 
                     variables = c("B01003_001"), 
                     state = "TX",
                     geometry = FALSE,
                     year = 2017)
## Getting data from the 2013-2017 5-year ACS
public.HI.pop <- inner_join(public.HI, pop, by = c("GEOID" = "GEOID", "NAME" = "NAME"))

# get percentage of population of county on public health insurance
public.HI.pop %<>%
  mutate(perc_HI = round(estimate.x / estimate.y, digits = 2))


# get tigris data on counties in Texas and merge with health insurance data frame
# Reference: https://github.com/walkerke/tigris
tx.counties <- counties(state = 48)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |                                                                 |   1%
  |                                                                       
  |=                                                                |   1%
  |                                                                       
  |=                                                                |   2%
  |                                                                       
  |==                                                               |   2%
  |                                                                       
  |==                                                               |   3%
  |                                                                       
  |==                                                               |   4%
  |                                                                       
  |===                                                              |   4%
  |                                                                       
  |===                                                              |   5%
  |                                                                       
  |====                                                             |   5%
  |                                                                       
  |====                                                             |   6%
  |                                                                       
  |====                                                             |   7%
  |                                                                       
  |=====                                                            |   7%
  |                                                                       
  |=====                                                            |   8%
  |                                                                       
  |======                                                           |   8%
  |                                                                       
  |======                                                           |   9%
  |                                                                       
  |======                                                           |  10%
  |                                                                       
  |=======                                                          |  10%
  |                                                                       
  |=======                                                          |  11%
  |                                                                       
  |=======                                                          |  12%
  |                                                                       
  |========                                                         |  12%
  |                                                                       
  |========                                                         |  13%
  |                                                                       
  |=========                                                        |  13%
  |                                                                       
  |=========                                                        |  14%
  |                                                                       
  |=========                                                        |  15%
  |                                                                       
  |==========                                                       |  15%
  |                                                                       
  |==========                                                       |  16%
  |                                                                       
  |===========                                                      |  16%
  |                                                                       
  |===========                                                      |  17%
  |                                                                       
  |===========                                                      |  18%
  |                                                                       
  |============                                                     |  18%
  |                                                                       
  |============                                                     |  19%
  |                                                                       
  |=============                                                    |  19%
  |                                                                       
  |=============                                                    |  20%
  |                                                                       
  |=============                                                    |  21%
  |                                                                       
  |==============                                                   |  21%
  |                                                                       
  |==============                                                   |  22%
  |                                                                       
  |===============                                                  |  22%
  |                                                                       
  |===============                                                  |  23%
  |                                                                       
  |===============                                                  |  24%
  |                                                                       
  |================                                                 |  24%
  |                                                                       
  |================                                                 |  25%
  |                                                                       
  |=================                                                |  25%
  |                                                                       
  |=================                                                |  26%
  |                                                                       
  |=================                                                |  27%
  |                                                                       
  |==================                                               |  27%
  |                                                                       
  |==================                                               |  28%
  |                                                                       
  |===================                                              |  28%
  |                                                                       
  |===================                                              |  29%
  |                                                                       
  |===================                                              |  30%
  |                                                                       
  |====================                                             |  30%
  |                                                                       
  |====================                                             |  31%
  |                                                                       
  |====================                                             |  32%
  |                                                                       
  |=====================                                            |  32%
  |                                                                       
  |=====================                                            |  33%
  |                                                                       
  |======================                                           |  33%
  |                                                                       
  |======================                                           |  34%
  |                                                                       
  |======================                                           |  35%
  |                                                                       
  |=======================                                          |  35%
  |                                                                       
  |=======================                                          |  36%
  |                                                                       
  |========================                                         |  36%
  |                                                                       
  |========================                                         |  37%
  |                                                                       
  |========================                                         |  38%
  |                                                                       
  |=========================                                        |  38%
  |                                                                       
  |=========================                                        |  39%
  |                                                                       
  |==========================                                       |  39%
  |                                                                       
  |==========================                                       |  40%
  |                                                                       
  |==========================                                       |  41%
  |                                                                       
  |===========================                                      |  41%
  |                                                                       
  |===========================                                      |  42%
  |                                                                       
  |============================                                     |  42%
  |                                                                       
  |============================                                     |  43%
  |                                                                       
  |============================                                     |  44%
  |                                                                       
  |=============================                                    |  44%
  |                                                                       
  |=============================                                    |  45%
  |                                                                       
  |==============================                                   |  45%
  |                                                                       
  |==============================                                   |  46%
  |                                                                       
  |==============================                                   |  47%
  |                                                                       
  |===============================                                  |  47%
  |                                                                       
  |===============================                                  |  48%
  |                                                                       
  |================================                                 |  48%
  |                                                                       
  |================================                                 |  49%
  |                                                                       
  |================================                                 |  50%
  |                                                                       
  |=================================                                |  50%
  |                                                                       
  |=================================                                |  51%
  |                                                                       
  |=================================                                |  52%
  |                                                                       
  |==================================                               |  52%
  |                                                                       
  |==================================                               |  53%
  |                                                                       
  |===================================                              |  53%
  |                                                                       
  |===================================                              |  54%
  |                                                                       
  |===================================                              |  55%
  |                                                                       
  |====================================                             |  55%
  |                                                                       
  |====================================                             |  56%
  |                                                                       
  |=====================================                            |  56%
  |                                                                       
  |=====================================                            |  57%
  |                                                                       
  |=====================================                            |  58%
  |                                                                       
  |======================================                           |  58%
  |                                                                       
  |======================================                           |  59%
  |                                                                       
  |=======================================                          |  59%
  |                                                                       
  |=======================================                          |  60%
  |                                                                       
  |=======================================                          |  61%
  |                                                                       
  |========================================                         |  61%
  |                                                                       
  |========================================                         |  62%
  |                                                                       
  |=========================================                        |  62%
  |                                                                       
  |=========================================                        |  63%
  |                                                                       
  |=========================================                        |  64%
  |                                                                       
  |==========================================                       |  64%
  |                                                                       
  |==========================================                       |  65%
  |                                                                       
  |===========================================                      |  65%
  |                                                                       
  |===========================================                      |  66%
  |                                                                       
  |===========================================                      |  67%
  |                                                                       
  |============================================                     |  67%
  |                                                                       
  |============================================                     |  68%
  |                                                                       
  |=============================================                    |  68%
  |                                                                       
  |=============================================                    |  69%
  |                                                                       
  |=============================================                    |  70%
  |                                                                       
  |==============================================                   |  70%
  |                                                                       
  |==============================================                   |  71%
  |                                                                       
  |==============================================                   |  72%
  |                                                                       
  |===============================================                  |  72%
  |                                                                       
  |===============================================                  |  73%
  |                                                                       
  |================================================                 |  73%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |================================================                 |  75%
  |                                                                       
  |=================================================                |  75%
  |                                                                       
  |=================================================                |  76%
  |                                                                       
  |==================================================               |  76%
  |                                                                       
  |==================================================               |  77%
  |                                                                       
  |==================================================               |  78%
  |                                                                       
  |===================================================              |  78%
  |                                                                       
  |===================================================              |  79%
  |                                                                       
  |====================================================             |  79%
  |                                                                       
  |====================================================             |  80%
  |                                                                       
  |====================================================             |  81%
  |                                                                       
  |=====================================================            |  81%
  |                                                                       
  |=====================================================            |  82%
  |                                                                       
  |======================================================           |  82%
  |                                                                       
  |======================================================           |  83%
  |                                                                       
  |======================================================           |  84%
  |                                                                       
  |=======================================================          |  84%
  |                                                                       
  |=======================================================          |  85%
  |                                                                       
  |========================================================         |  85%
  |                                                                       
  |========================================================         |  86%
  |                                                                       
  |========================================================         |  87%
  |                                                                       
  |=========================================================        |  87%
  |                                                                       
  |=========================================================        |  88%
  |                                                                       
  |==========================================================       |  88%
  |                                                                       
  |==========================================================       |  89%
  |                                                                       
  |==========================================================       |  90%
  |                                                                       
  |===========================================================      |  90%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |===========================================================      |  92%
  |                                                                       
  |============================================================     |  92%
  |                                                                       
  |============================================================     |  93%
  |                                                                       
  |=============================================================    |  93%
  |                                                                       
  |=============================================================    |  94%
  |                                                                       
  |=============================================================    |  95%
  |                                                                       
  |==============================================================   |  95%
  |                                                                       
  |==============================================================   |  96%
  |                                                                       
  |===============================================================  |  96%
  |                                                                       
  |===============================================================  |  97%
  |                                                                       
  |===============================================================  |  98%
  |                                                                       
  |================================================================ |  98%
  |                                                                       
  |================================================================ |  99%
  |                                                                       
  |=================================================================|  99%
  |                                                                       
  |=================================================================| 100%
tx.counties@data <- merge(tx.counties@data, public.HI.pop, sort = FALSE, by = "GEOID")

# create palettes of colors
# Reference: https://rstudio.github.io/leaflet/colors.html
pal <- colorBin(palette = "Oranges",
                bins = c(0, .05, 0.1, 0.15, 0.2, 0.25, .3, .35, .4),
                domain = tx.counties$perc_HI)

pal2 <- colorBin(palette = "PuRd",
                bins = c(0, .05, 0.1, 0.15, 0.2, 0.25, .3, .35, .4),
                domain = tx.counties$perc_HI)


leaflet(tx.counties) %>%
  addProviderTiles("OpenStreetMap") %>% 
  # Orange shading of public health insurance
  addPolygons(fillColor = ~pal(perc_HI),
              weight = 1,
              opacity = 1,
              color = "black",
              fillOpacity = 0.6,
              popup = ~paste0("<b>", NAMELSAD, "</b> ",
                              "<br>Residents: ", format(estimate.y, big.mark = "," ),
                              "<br>Residents on public health insurance: ", format(estimate.x,big.mark = ","),
                              "<br>Percent of residents on public<br>health insurance: ",
                              perc_HI * 100, "%"),
              group = "Orange") %>%
  # Purple shading of public health insurance
  addPolygons(fillColor = ~pal2(perc_HI),
              weight = 1,
              opacity = 1,
              color = "black",
              fillOpacity = 0.6,
              popup = ~paste0("<b>", NAMELSAD, "</b> ",
                              "<br>Residents: ", format(estimate.y, big.mark = "," ),
                              "<br>Residents on public health insurance: ", format(estimate.x,big.mark = ","),
                              "<br>Percent of residents on public<br>health insurance: ",
                              perc_HI * 100, "%"),
              group = "Purple") %>%
  # Reference: https://rstudio.github.io/leaflet/legends.html
  # Reference: https://stackoverflow.com/questions/38701359/grouped-layer-control-in-leaflet-r
  # Reference: https://rstudio.github.io/leaflet/showhide.html
  addLegend(position = "bottomright",
            pal = pal,
            values = ~perc_HI,
            title = "Percent of county residents<br>on public health insurance",
            labFormat = labelFormat(digits = 0, transform = function(x) 100 * x, suffix = "%" ),
            group = "Orange") %>%
  addLegend(position = "bottomright",
            pal = pal2,
            values = ~perc_HI,
            title = "Percent of county residents<br>on public health insurance",
            labFormat = labelFormat(digits = 0, transform = function(x) 100 * x, suffix = "%" ),
            group = "Purple") %>%
  # allow user to change color of shading and legend
  addLayersControl(overlayGroups = c("Orange", "Purple"),
                   options = layersControlOptions(collapsed=FALSE)) %>%
  # by default, hide the group "Purple"
  hideGroup("Purple")

Natural Gas Pipelines in the United States

# data source: https://hifld-geoplatform.opendata.arcgis.com/datasets/natural-gas-pipelines?geometry=-109.914%2C26.933%2C-86.249%2C33.570

pipelines <- readOGR("./Natural_Gas_Pipelines/Natural_Gas_Pipelines.shp")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\RShiny\mkennady-hw2\Natural_Gas_Pipelines\Natural_Gas_Pipelines.shp", layer: "Natural_Gas_Pipelines"
## with 33806 features
## It has 5 fields
## Integer64 fields read as strings:  FID
## Warning in readOGR("./Natural_Gas_Pipelines/Natural_Gas_Pipelines.shp"):
## Dropping null geometries: 4672, 4874, 4876, 5328, 7134, 7135, 8418, 8429,
## 16135, 16136, 16695, 16696, 16697, 16699, 16700, 17491, 17492, 17493,
## 17494, 17498, 17502, 17503, 22753, 25022, 25028, 25031, 25034, 25038,
## 25039, 25938, 28362, 28363, 28365, 28366, 28369, 31296, 31299, 31300,
## 31301, 31302, 31303, 31304, 31305, 31306, 31307, 31308, 31309, 31310,
## 31311, 31312, 31316, 31317, 31318, 31321, 31322, 31326, 31328, 31329,
## 31330, 31337, 31343, 31406, 31407, 31408, 31409, 31410, 31411, 31416,
## 31417, 31418, 31419, 31420, 31421, 31422, 31423, 31424, 31425, 31426,
## 31431, 31434, 31435, 31436, 31437, 31440, 31441, 31442, 31443, 31444,
## 31445, 31446, 31447, 31452, 31453, 31454, 31455, 31456, 31457, 31470,
## 31471, 31472, 31473, 31474, 31475, 31476, 31477, 31478, 31479, 31481,
## 31482, 31483, 31542, 31559, 31621, 31623, 31626, 31628, 31655, 31656,
## 31657, 31658, 31659, 31660, 31661, 31662, 31663, 31664, 31665, 31666,
## 31667, 31668, 31669, 31670, 31671, 31672, 31673, 31674, 31675, 31676,
## 31705, 31712, 31713, 31714, 31715, 31716, 31717, 31718, 31733, 31734,
## 31735, 31736, 31739, 31746, 31756, 31760, 31761, 31762, 31763, 31764,
## 31779, 31789, 31790, 31791, 31809
leaflet(pipelines) %>%
  addProviderTiles("OpenStreetMap.Mapnik") %>%
  addPolylines(color = "red",
              weight = 1, 
              group = "Red")

Residents of Texas Counties working in Mining, Quarrying, and Oil and Gas Extraction

Source: U.S. Census Bureau, ACS 5-year, Table C24040 Table: SEX BY INDUSTRY FOR THE FULL-TIME, YEAR-ROUND CIVILIAN EMPLOYED POPULATION 16 YEARS AND OVER, https://data.census.gov/cedsci/table?hidePreview=true&q=C24040

# data source: https://hifld-geoplatform.opendata.arcgis.com/datasets/oil-refinieries-1
refineries <- read_csv("./Oil_Refineries.csv")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   REF_ID = col_character(),
##   NAME = col_character(),
##   ADDRESS = col_character(),
##   CITY = col_character(),
##   STATE = col_character(),
##   ZIP4 = col_character(),
##   TELEPHONE = col_character(),
##   TYPE = col_character(),
##   STATUS = col_character(),
##   COUNTY = col_character(),
##   COUNTRY = col_character(),
##   NAICS_DESC = col_character(),
##   SOURCE = col_character(),
##   SOURCEDATE = col_datetime(format = ""),
##   VAL_METHOD = col_character(),
##   VAL_DATE = col_datetime(format = ""),
##   WEBSITE = col_character(),
##   OWNER = col_character(),
##   OPERNAME = col_character(),
##   RMP_ID = col_character()
##   # ... with 2 more columns
## )
## See spec(...) for full column specifications.
oil <- get_acs(geography = "county", 
                     variables = c("C24040_005"), 
                     state = "TX",
                     geometry = FALSE,
                     year = 2017)
## Getting data from the 2013-2017 5-year ACS
oilFemale <- get_acs(geography = "county", 
                     variables = c("C24040_032"), 
                     state = "TX",
                     geometry = FALSE,
                     year = 2017)
## Getting data from the 2013-2017 5-year ACS
# merge datasets and get percentage of each county's workers in mining/oil industry
oil %<>%
  left_join(oilFemale, by = c("GEOID" = "GEOID", "NAME" = "NAME")) %>%
  left_join(pop, by = c("GEOID" = "GEOID", "NAME" = "NAME")) %>%
  mutate(total_oil = estimate.x + estimate.y, perc_oil = total_oil / estimate)

oil$perc_oil <- as.numeric(round(oil$perc_oil, digits = 2))  * 100

counties.oil <- counties(state = 48)

counties.oil@data <- merge(counties.oil@data, oil, sort = FALSE, by = "GEOID")


pal3 <- colorBin(palette = "Oranges",
                bins = c(0, 2, 4, 6, 8, 12, 14),
                domain = counties.oil$perc_oil)


leaflet(counties.oil) %>%
  addProviderTiles("OpenStreetMap", group = "OSM") %>% 
  addProviderTiles("Stamen.Terrain", group = "Stamen") %>% 
  addPolygons(fillColor = ~pal3(perc_oil),
              weight = 1,
              opacity = 1,
              color = "black",
              fillOpacity = 0.6,
              popup = ~paste0("<b>", NAMELSAD, "</b> ",
                              "<br>Residents working in oil & gas: ", format(total_oil, big.mark = ","),
                              "<br>Percent of residents working in oil & gas: ", perc_oil, "%"),
              group = "Oil/Gas Workers") %>%
  addMarkers(data = refineries, 
             lng = ~LONGITUDE, 
             lat = ~LATITUDE, 
             popup = paste0(refineries$OPERNAME, "<br>", 
                            refineries$ADDRESS, "<br>", 
                            refineries$CITY, ", ", 
                            refineries$STATE, " ", 
                            refineries$ZIP),
             group = "Refineries") %>%
  addLegend(position = "bottomright",
            pal = pal3,
            values = ~perc_oil,
            title = "Percent of residents working in oil & gas",
            labFormat = labelFormat(digits = 0, suffix = "%" )) %>%
  addLayersControl(baseGroups = c("OSM", "Stamen"),
                   overlayGroups = c("Oil/Gas Workers","Refineries"),
                   options = layersControlOptions(collapsed = FALSE))